
// src/main/java/com/xxxy/ljh/myapplication/DaoImpl/ItemDaoImpl.java
package com.xxxy.ljh.myapplication.DaoImpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xxxy.ljh.myapplication.Dao.ItemDao;
import com.xxxy.ljh.myapplication.dphelper.sqliteDBHelper;
import com.xxxy.ljh.myapplication.model.Item;

import java.util.ArrayList;
import java.util.List;

/**
 * 物品数据访问接口实现类，用于操作物品表（item）的数据。
 */
public class ItemDaoImpl implements ItemDao {

    private Context mContext;
    private SQLiteDatabase mDatabase;

    /**
     * 构造方法，初始化数据库连接。
     *
     * @param context 上下文对象，用于创建数据库帮助类实例
     */
    public ItemDaoImpl(Context context) {
        mContext = context;
        mDatabase = new sqliteDBHelper(mContext).getWritableDatabase();
    }

    /**
     * 添加一个新的物品记录到数据库中。
     *
     * @param item 要添加的物品对象
     * @return 返回插入操作的结果ID，如果失败则返回-1
     */
    @Override
    public long addItem(Item item) {
        ContentValues values = new ContentValues();
        values.put("association_id", item.getAssociationId());
        values.put("item_name", item.getItemName());
        values.put("item_type", item.getItemType());
        values.put("specification", item.getSpecification());
        values.put("quantity", item.getQuantity());
        values.put("available_quantity", item.getAvailableQuantity());
        values.put("status", item.getStatus());
        values.put("purchase_time", item.getPurchaseTime());
        values.put("purchase_price", item.getPurchasePrice());
        values.put("total_value", item.getTotalValue());
        values.put("manager_id", item.getManagerId());
        values.put("storage_location", item.getStorageLocation());
        values.put("use_rule", item.getUseRule());
        values.put("remark", item.getRemark());

        long result = mDatabase.insert("item", null, values);
        return result;
    }

    /**
     * 根据物品ID查询对应的物品信息。
     *
     * @param itemId 物品唯一标识符
     * @return 查询到的物品对象，若未找到则返回null
     */
    @Override
    public Item getItemById(int itemId) {
        String selection = "item_id = ?";
        String[] selectionArgs = {String.valueOf(itemId)};
        Cursor cursor = mDatabase.query("item", null, selection, selectionArgs, null, null, null);

        Item item = null;
        if (cursor.moveToFirst()) {
            item = new Item();
            item.setItemId(cursor.getInt(cursor.getColumnIndexOrThrow("item_id")));
            item.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            item.setItemName(cursor.getString(cursor.getColumnIndexOrThrow("item_name")));
            item.setItemType(cursor.getString(cursor.getColumnIndexOrThrow("item_type")));
            item.setSpecification(cursor.getString(cursor.getColumnIndexOrThrow("specification")));
            item.setQuantity(cursor.getInt(cursor.getColumnIndexOrThrow("quantity")));
            item.setAvailableQuantity(cursor.getInt(cursor.getColumnIndexOrThrow("available_quantity")));
            item.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            item.setPurchaseTime(cursor.getString(cursor.getColumnIndexOrThrow("purchase_time")));
            item.setPurchasePrice(cursor.getDouble(cursor.getColumnIndexOrThrow("purchase_price")));
            item.setTotalValue(cursor.getDouble(cursor.getColumnIndexOrThrow("total_value")));
            item.setManagerId(cursor.getInt(cursor.getColumnIndexOrThrow("manager_id")));
            item.setStorageLocation(cursor.getString(cursor.getColumnIndexOrThrow("storage_location")));
            item.setUseRule(cursor.getString(cursor.getColumnIndexOrThrow("use_rule")));
            item.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
        }
        cursor.close();
        return item;
    }

    /**
     * 根据社团ID获取该社团下的所有物品列表。
     *
     * @param associationId 社团唯一标识符
     * @return 包含所有匹配物品的列表
     */
    @Override
    public List<Item> getItemsByAssociationId(int associationId) {
        String selection = "association_id = ?";
        String[] selectionArgs = {String.valueOf(associationId)};
        Cursor cursor = mDatabase.query("item", null, selection, selectionArgs, null, null, null);

        List<Item> items = new ArrayList<>();
        while (cursor.moveToNext()) {
            Item item = new Item();
            item.setItemId(cursor.getInt(cursor.getColumnIndexOrThrow("item_id")));
            item.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            item.setItemName(cursor.getString(cursor.getColumnIndexOrThrow("item_name")));
            item.setItemType(cursor.getString(cursor.getColumnIndexOrThrow("item_type")));
            item.setSpecification(cursor.getString(cursor.getColumnIndexOrThrow("specification")));
            item.setQuantity(cursor.getInt(cursor.getColumnIndexOrThrow("quantity")));
            item.setAvailableQuantity(cursor.getInt(cursor.getColumnIndexOrThrow("available_quantity")));
            item.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            item.setPurchaseTime(cursor.getString(cursor.getColumnIndexOrThrow("purchase_time")));
            item.setPurchasePrice(cursor.getDouble(cursor.getColumnIndexOrThrow("purchase_price")));
            item.setTotalValue(cursor.getDouble(cursor.getColumnIndexOrThrow("total_value")));
            item.setManagerId(cursor.getInt(cursor.getColumnIndexOrThrow("manager_id")));
            item.setStorageLocation(cursor.getString(cursor.getColumnIndexOrThrow("storage_location")));
            item.setUseRule(cursor.getString(cursor.getColumnIndexOrThrow("use_rule")));
            item.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
            items.add(item);
        }
        cursor.close();
        return items;
    }

    /**
     * 更新指定物品的信息。
     *
     * @param item 需要更新的物品对象
     * @return 操作是否成功
     */
    @Override
    public boolean updateItem(Item item) {
        ContentValues values = new ContentValues();
        values.put("association_id", item.getAssociationId());
        values.put("item_name", item.getItemName());
        values.put("item_type", item.getItemType());
        values.put("specification", item.getSpecification());
        values.put("quantity", item.getQuantity());
        values.put("available_quantity", item.getAvailableQuantity());
        values.put("status", item.getStatus());
        values.put("purchase_time", item.getPurchaseTime());
        values.put("purchase_price", item.getPurchasePrice());
        values.put("total_value", item.getTotalValue());
        values.put("manager_id", item.getManagerId());
        values.put("storage_location", item.getStorageLocation());
        values.put("use_rule", item.getUseRule());
        values.put("remark", item.getRemark());

        String whereClause = "item_id = ?";
        String[] whereArgs = {String.valueOf(item.getItemId())};

        int result = mDatabase.update("item", values, whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 删除指定ID的物品记录。
     *
     * @param itemId 要删除的物品ID
     * @return 操作是否成功
     */
    @Override
    public boolean deleteItem(int itemId) {
        String whereClause = "item_id = ?";
        String[] whereArgs = {String.valueOf(itemId)};

        int result = mDatabase.delete("item", whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 获取所有的物品记录。
     *
     * @return 所有物品组成的列表
     */
    @Override
    public List<Item> getAllItems() {
        Cursor cursor = mDatabase.query("item", null, null, null, null, null, null);

        List<Item> items = new ArrayList<>();
        while (cursor.moveToNext()) {
            Item item = new Item();
            item.setItemId(cursor.getInt(cursor.getColumnIndexOrThrow("item_id")));
            item.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            item.setItemName(cursor.getString(cursor.getColumnIndexOrThrow("item_name")));
            item.setItemType(cursor.getString(cursor.getColumnIndexOrThrow("item_type")));
            item.setSpecification(cursor.getString(cursor.getColumnIndexOrThrow("specification")));
            item.setQuantity(cursor.getInt(cursor.getColumnIndexOrThrow("quantity")));
            item.setAvailableQuantity(cursor.getInt(cursor.getColumnIndexOrThrow("available_quantity")));
            item.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            item.setPurchaseTime(cursor.getString(cursor.getColumnIndexOrThrow("purchase_time")));
            item.setPurchasePrice(cursor.getDouble(cursor.getColumnIndexOrThrow("purchase_price")));
            item.setTotalValue(cursor.getDouble(cursor.getColumnIndexOrThrow("total_value")));
            item.setManagerId(cursor.getInt(cursor.getColumnIndexOrThrow("manager_id")));
            item.setStorageLocation(cursor.getString(cursor.getColumnIndexOrThrow("storage_location")));
            item.setUseRule(cursor.getString(cursor.getColumnIndexOrThrow("use_rule")));
            item.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
            items.add(item);
        }
        cursor.close();
        return items;
    }

    /**
     * 更新指定物品的数量和可用数量字段。
     *
     * @param itemId           物品ID
     * @param quantity         总数量
     * @param availableQuantity 可用数量
     * @return 操作是否成功
     */
    @Override
    public boolean updateItemQuantity(int itemId, int quantity, int availableQuantity) {
        ContentValues values = new ContentValues();
        values.put("quantity", quantity);
        values.put("available_quantity", availableQuantity);

        String whereClause = "item_id = ?";
        String[] whereArgs = {String.valueOf(itemId)};

        int result = mDatabase.update("item", values, whereClause, whereArgs);
        return result > 0;
    }
}